package com.googlecode.iiddefecttracker.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.googlecode.iiddefecttracker.domain.Defect;
import com.googlecode.iiddefecttracker.service.DefectService;

/**
 * 
 */
public class ViewDefectController implements Controller {
    
    private final DefectService defectService;
    
    public ViewDefectController(final DefectService defectService) {
        this.defectService = defectService;
    }
    
    public ModelAndView handleRequest(final HttpServletRequest request,
            final HttpServletResponse response) throws Exception {
        
        final Long defectId = Long.valueOf(request.getParameter("id"));
        
        final Defect defect = defectService.findDefectById(defectId);
        
        final Map<String, Object> defectPageModel = new HashMap<String, Object>();
        defectPageModel.put("defectToView", defect);
        
        return new ModelAndView("viewDefect", "model", defectPageModel);
    }
    
}
